package edu.xidian.bdilab.jrbigdata.service.bank.bankStatics.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xidian.bdilab.jrbigdata.controller.bank.BankCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.FinancialProduct;
import edu.xidian.bdilab.jrbigdata.domain.ProductRequest;
import edu.xidian.bdilab.jrbigdata.mapper.FinancialProductMapper;
import edu.xidian.bdilab.jrbigdata.service.bank.bankStatics.BankStaticsService;
import edu.xidian.bdilab.jrbigdata.service.bank.productApplyCheck.ProductApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangmmm
 * @create 2020-12-04 16:50
 */
@Service
public class BankStaticsServiceImpl implements BankStaticsService {

    @Autowired
    FinancialProductMapper financialProductMapper;

    @Autowired
    ProductApplyService productApplyService;

    /*
    * 获得产品贷款额度
    */
    @Override
    public Integer getProductLoanLimit(FinancialProduct financialProduct){
        Integer loanlimit;
        switch (financialProduct.getLoanLimit()) {
            case 0:
                loanlimit = 100;
                break;
            case 1:
                loanlimit = 200;
                break;
            case 2:
                loanlimit = 300;
                break;
            case 3:
                loanlimit = 500;
                break;
            default:
                loanlimit = 1000;
                break;
        }
        return loanlimit;
    }

    @Override
    public ResponseResult getBankStatics(String bankName) {
        ResponseResult responseResult = new ResponseResult();
        Map<String,Object> data=new HashMap<>(9);
        //本银行的金融产品总数量
        QueryWrapper<FinancialProduct> financialProductQueryWrapper = new QueryWrapper<>();
        financialProductQueryWrapper.eq("bank_name",bankName);
        List<FinancialProduct> financialProductList = financialProductMapper.selectList(financialProductQueryWrapper);
        Integer bankProductCount = financialProductList.size();
        //所有产品累计贷款额度
        Integer productAmountCount =0;
        if(bankProductCount !=0){
            for(FinancialProduct financialProduct: financialProductList) {
                productAmountCount+=getProductLoanLimit(financialProduct);
            }
        }
        //本银行所有的产品请求数量
        List<ProductRequest> allProRequest = productApplyService.getProRequest(bankName);
        Integer bankProductApplyCount = allProRequest.size();
        //成交，正在申请，已拒绝的数量及累计金额
        Integer dealCount=0;
        Integer dealAmountCount=0;
        Integer applyButNotDealCount=0;
        Integer applyButNotDealAmountCount=0;
        Integer refuseCount=0;
        Integer refuseAmountCount=0;
        for (ProductRequest productRequest:allProRequest) {
            QueryWrapper<FinancialProduct> ProductQueryWrapper = new QueryWrapper<>();
            ProductQueryWrapper.eq("product_name",productRequest.getFkProductName());
            FinancialProduct Product= financialProductMapper.selectOne(ProductQueryWrapper);
            switch (productRequest.getStatus()) {
                case 0:
                    applyButNotDealCount += 1;
                    applyButNotDealAmountCount += getProductLoanLimit(Product);
                    break;
                case 1:
                    dealCount += 1;
                    dealAmountCount += getProductLoanLimit(Product);
                    break;
                case 2:
                    refuseCount += 1;
                    refuseAmountCount += getProductLoanLimit(Product);
                    break;
                default:
                    break;
            }
        }

        data.put("bankProductCount", bankProductCount);
        data.put("productAmountCount", productAmountCount);
        data.put("bankProductApplyCount", bankProductApplyCount);
        data.put("dealCount", dealCount);
        data.put("dealAmountCount", dealAmountCount);
        data.put("applyButNotDealCount", applyButNotDealCount);
        data.put("applyButNotDealAmountCount", applyButNotDealAmountCount);
        data.put("refuseCount", refuseCount);
        data.put("refuseAmountCount", refuseAmountCount);
        responseResult.setData(data);
        responseResult.setMessage(BankCode.BANK_STATICS_SUCCESS.getMessage());
        responseResult.setSuccess(true);
        responseResult.setCode(BankCode.BANK_STATICS_FAILED.getCode());
        return responseResult;
    }
}
